package com.kk.network.httpclient;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class HttpPostDownloadFile {

	/**
	 * 实例对象
	 */
	public static final HttpPostDownloadFile DOWNINSTANCE = new HttpPostDownloadFile();

	/**
	 * 使用get方式进行文件的下载操作
	 * 
	 * @param url           URL信息
	 * @param filelocalName 本地文件名
	 */
	public int getDownLoadFile(String url, String filelocalName) {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		FileOutputStream output = null;

		int result = 0;

		try {
			// 创建httpget.
			HttpGet httpget = new HttpGet(url);

			output = new FileOutputStream(filelocalName);

			byte[] buffbyte = new byte[1024];
			// 执行get请求.
			response = httpclient.execute(httpget);

			if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
				// 获取响应实体
				HttpEntity entity = response.getEntity();
				int index = 0;

				while ((index = entity.getContent().read(buffbyte)) != -1) {
					output.write(buffbyte, 0, index);
				}

				result = HttpStatus.SC_OK;
			} else {
				response.getStatusLine().getStatusCode();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			if (null != httpclient) {
				try {
					httpclient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			// 关闭输出流
			if (null != output) {
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			// 关闭响应
			if (null != response) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		System.out.println(url + " file download over" + response.getStatusLine().getStatusCode());

		return result;
	}

	/**
	 * 进行文件的下载合并操作
	 * 
	 * @param minute      分钟数
	 * @param second      秒数
	 * @param url         URL地址信息
	 * @param outFilePath 输出的地址
	 * @param margeName   合并最终的文件名
	 */
	public void downloadFiles(int minute, int second, String url, String outBaseFilePath, String name) {
		int sum = minute * 60 + second;
		int count = 0;

		final int playLength = 10;
		final String BASEHEAD = "ld-";
		final String SUFFIXNAME = ".ts";
		final int formatLength = 5;

		if (sum % playLength == 0) {
			count = sum / playLength + 1;
		} else {
			count = sum / playLength + 2;
		}

		// 检查输出目录
		File margeFile = new File(outBaseFilePath + File.separator + name);
		if (!margeFile.exists()) {
			margeFile.mkdirs();
		}

		String outFilePath = margeFile.getPath();

		// 文件下载操作
		for (int i = 1; i <= count; i++) {
			String downloadName = FileNameCount.countName(BASEHEAD, i, formatLength, SUFFIXNAME);
			DOWNINSTANCE.getDownLoadFile(url + downloadName, outFilePath + File.separator + downloadName);
		}

		// 进行文件的合并操作
		for (int i = 1; i <= count; i++) {
			String localFileName = FileNameCount.countName(BASEHEAD, i, formatLength, SUFFIXNAME);
			FileMarge.INSTANCE.fileCopyAppend(outFilePath + File.separator + localFileName,
					outFilePath + File.separator + name + SUFFIXNAME);
		}

		// 删除中间文件
		for (int i = 1; i <= count; i++) {
			String localFileName = FileNameCount.countName(BASEHEAD, i, formatLength, SUFFIXNAME);
			new File(outFilePath + File.separator + localFileName).delete();
		}

	}

	

}
